package com.hns.can.energy.web;


import java.net.URLEncoder;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hns.can.base.entity.SysUserEntity;
import com.hns.can.common.domain.GridPage;
import com.hns.can.common.service.CommonInitParamService;
import com.hns.can.common.web.BaseController;
import com.hns.can.energy.service.DrivingMileageService;
import com.hns.can.energy.vo.NewEnergy;
import com.hns.frame.mybatis.Pagination;
import com.hns.tool.export.ExportUtil;
import com.hns.tool.pub.DateTool;
import com.hns.tool.pub.PubUtil;
/**
 * 驾驶员里程controller
 * @author:zhangy
 * @email:zhangyu2222z@sina.cn
 * @version Revision 2.0.0
 */
@Controller
@RequestMapping("drivingMileage")
public class DrivingMileageController extends BaseController { 
	
	final String[] headerNames = {"驾驶员", "车牌号", "车辆编号", "营运线路", "所属机构", "车辆类型", 
	        "电机型号", "运营日期", "运行时长", "运行里程"};
	    
	final String[] headerFields={"drvName","licPltNo","carInCd","lineName","organName",
	        "carType","engiNo","days","rngOprtDuraTr","rngMile"};

	@Autowired
    private CommonInitParamService commonInitParamService;
	
	@Autowired
	private DrivingMileageService drivingMileageService;
	
	/**
	 * 查询驾驶员里程报表
	 * @param vo
	 * @return
	 * @throws Exception
	 * @author:zhangy
	 * @email:zhangyu2222z@sina.cn
	 */
	@RequestMapping("searchDrivingMileage")
	@ResponseBody
	public Object searchDrivingMileage(NewEnergy vo) throws Exception{
		if(PubUtil.isNotEmpty(vo.getDateType()) && PubUtil.isNotEmpty(vo.getEnergyType())){
    		commonInitParamService.dateType2DateTime(vo, vo.getDateType());
    	}
    	
        if (vo.getOrgIds() == null || vo.getBeginTime() == null || vo.getEndTime() == null) {
            return this.getReturnResult();
        }
        SysUserEntity user = this.getCurrentUser().getUser();
        vo.setCorpId(user.getCorpId());
        vo.setDriverRelTableName("T_COMM_ENER_CONS_STAT_INFO");
		Pagination page = drivingMileageService.searchDrivingMileage(vo);
		GridPage grid = new GridPage(page);
		return this.getReturnResult(grid);
	}
	
	/**
	 * 导出驾驶员里程对比excel
	 * @param response
	 * @param vo
	 * @throws Exception
	 * @author:zhangy
	 * @email:zhangyu2222z@sina.cn
	 */
	@RequestMapping("exportDrivingMileageExcel")
	public void exportDrivingMileageExcel(HttpServletResponse response, NewEnergy vo) throws Exception{
		List list = getDrivingMileage(vo);
		ExportUtil.exportFiles(Arrays.asList(headerNames), Arrays.asList(headerFields), list, response, 
				"excel", URLEncoder.encode("驾驶员里程明细", "UTF-8")+DateTool.getCurrentSysDate("yyyyMMddHHmmss"), "驾驶员里程");
	}
	
	/**
	 * 导出驾驶员里程对比pdf
	 * @param response
	 * @param vo
	 * @throws Exception
	 * @author:zhangy
	 * @email:zhangyu2222z@sina.cn
	 */
	@RequestMapping("exportDrivingMileagePdf")
	public void exportDrivingMileagePdf(HttpServletResponse response, NewEnergy vo) throws Exception{
		List list = getDrivingMileage(vo);
		ExportUtil.exportFiles(Arrays.asList(headerNames), Arrays.asList(headerFields), list, response, 
				"pdf", URLEncoder.encode("驾驶员里程明细", "UTF-8")+DateTool.getCurrentSysDate("yyyyMMddHHmmss"), "驾驶员里程");
	}
	
	/**
	 * 导出驾驶员里程数据
	 * @param vo
	 * @return
	 * @throws Exception
	 * @author:zhangy
	 * @email:zhangyu2222z@sina.cn
	 */
	private List getDrivingMileage(NewEnergy vo) throws Exception{
		if(PubUtil.isNotEmpty(vo.getDateType()) && PubUtil.isNotEmpty(vo.getEnergyType())){
    		commonInitParamService.dateType2DateTime(vo, vo.getDateType());
    	}
    	
        if (vo.getOrgIds() == null || vo.getBeginTime() == null || vo.getEndTime() == null) {
            return null;
        }
        SysUserEntity user = this.getCurrentUser().getUser();
        vo.setCorpId(user.getCorpId());
        vo.setDriverRelTableName("T_COMM_ENER_CONS_STAT_INFO");
		List list = drivingMileageService.getDrivingMileage(vo);
		return list;
	}
	
}
